//
//  FYIntroViewController.m
//  MockProject
//
//  Created by FanYu on 8/20/16.
//  Copyright © 2016 niuzhaowang. All rights reserved.
//

#import "YXIntroViewController.h"
#import "YXTabbarController.h"
#import "Config.h"
#import "YXGuideView.h"
#import "YXIntroModel.h"

#define page 3

@interface YXIntroViewController () <UIScrollViewDelegate, YXGuideViewDelegate>

@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *introDataArray;

@end

@implementation YXIntroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configData];
    [self setupScrollView];
    [self setUpPageControl];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (BOOL)prefersStatusBarHidden {
    return YES;
}

#pragma mark - Setup UI
- (void)setupScrollView {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    scrollView.pagingEnabled = YES;
    scrollView.contentSize = CGSizeMake(SCREEN_WIDTH * page, 0);
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    
    for (int i = 0; i < page; i++) {
        YXGuideView *guideView = [[YXGuideView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        guideView.delegate = self;
        [guideView setupWithGuideModel:[self.introDataArray objectAtIndex:i]];
        [scrollView addSubview:guideView];
    }

    [self.view addSubview:scrollView];
}

- (void)setUpPageControl {
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = page;
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithHexString:@"989898"];
    pageControl.pageIndicatorTintColor = [UIColor colorWithHexString:@"e2e2e2"];
    [self.view addSubview:pageControl];
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        if ([SDVersion deviceSize] == Screen3Dot5inch) {
            make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        } else {
            make.bottom.equalTo(self.view.mas_bottom).offset(-37);
        }
    }];
    
    self.pageControl = pageControl;
}


#pragma mark - Helper 
- (void)configData {
    self.introDataArray = [[NSMutableArray alloc] init];
    
    NSArray *titles = @[@"泡泡导航 大改版", @"热门频道 在首页", @"最全最热 名星视频"];
    NSArray *details = @[@"切换操纵更加方便", @"精彩视频一键直达", @"等你来观看哦"];
    NSArray *colors = @[@"20c77a", @"f0a400", @"08bbd5"];
    NSRange range = NSMakeRange(0, 4);
    
    for (int i =0; i < page; i++) {
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:[titles objectAtIndex:i]];
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:[colors objectAtIndex:i]] range:range];
        [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFang SC" size:20] range:NSMakeRange(0, [[titles objectAtIndex:i] length])];
        
        YXIntroModel *introModel = [[YXIntroModel alloc] init];
        introModel.imageString = [NSString stringWithFormat:@"轮播图插图%d", i + 1];
        introModel.titleString = attributedString;
        introModel.detailString = [details objectAtIndex:i];
        introModel.isHiddenbutton = i == page - 1 ? NO : YES;
        [self.introDataArray addObject:introModel];
    }
}


#pragma mark - Actions
- (void)startButtonTapped {
    [UIApplication sharedApplication].keyWindow.rootViewController = [[YXTabbarController alloc] init];
}


#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat pageNumber = scrollView.contentOffset.x / SCREEN_WIDTH;
    int currentPage = (int)(pageNumber + 0.5);
    self.pageControl.currentPage = currentPage;
}

@end
